package com.mechat.ui.start

import UserUtil
import android.animation.ObjectAnimator
import android.os.Build
import android.util.Log
import android.view.View
import android.view.animation.AnticipateInterpolator
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.animation.doOnEnd
import androidx.core.splashscreen.SplashScreen
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.airbnb.lottie.LottieAnimationView
import com.mechat.Constant
import com.mechat.R
import io.rong.imkit.SpName
import com.mechat.entity.IMTokenGetEntity
import com.mechat.rongyun.RongConfigUtil
import io.rong.imkit.utils.FirebaseEventUtils
import com.mechat.util.SystemUtils
import com.custom.base.config.BaseConfig
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.custom.base.manager.SDActivityManager
import com.custom.base.mvp.MVPBaseActivity
import com.custom.base.util.IntentUtil
import com.custom.base.util.SDHandlerUtil
import com.custom.base.util.ToastUtil
import com.mechat.ui.HomeActivity
import kotlinx.android.synthetic.main.act_start.*


/**
 * 作者：
 * 时间：
 * 描述：启动页
 */

class StartActivity : MVPBaseActivity<StartContract.View, StartPresenter>(), StartContract.View {

    override fun setFullScreen(): Boolean {
        return true
    }

    override fun bindLayout(): Int {
        return R.layout.act_start
    }

    override fun initView() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            val installSplashScreen = installSplashScreen()
            installSplashScreen.setKeepOnScreenCondition(object :
                SplashScreen.KeepOnScreenCondition {
                override fun shouldKeepOnScreen(): Boolean {
                    return true
                }

            })
            splashScreen.setOnExitAnimationListener { splashScreenView ->
                if (splashScreenView.iconView == null) {
                    Log.e("~~~", "initView:null ")
                } else {
                    Log.e("~~~", "initView:no-null ")
                }

                // Create your custom animation.
                val slideUp = ObjectAnimator.ofFloat(
                    splashScreenView,
                    View.TRANSLATION_Y,
                    0f,
                    -splashScreenView.height.toFloat()
                )
                slideUp.interpolator = AnticipateInterpolator()
                slideUp.duration = 1000L
                // Call SplashScreenView.remove at the end of your custom animation.
                slideUp.doOnEnd { splashScreenView.remove() }
                // Run your animation.
                slideUp.start()
            }
        }
        setRongConfig()
        FirebaseEventUtils.init()

        if (BaseConfig.getInstance.getString(SpName.token, "") == "") {
            UserUtil.startLogin(mActivity)
        } else {
            if (SystemUtils.isConnected(mActivity)) {
                networkErrorContainer.visibility = View.GONE
                IntentUtil.startActivity(HomeActivity::class.java)
                SDActivityManager.instance.finishAllActivityExcept(HomeActivity::class.java)
            } else {
                networkErrorContainer.visibility = View.VISIBLE
                networkErrorTryAgain.setOnClickListener {
                    if (SystemUtils.isConnected(mActivity)) {
                        IntentUtil.startActivity(HomeActivity::class.java)
                        SDActivityManager.instance.finishAllActivityExcept(HomeActivity::class.java)
                    } else {
                        ToastUtil.toast(getString(R.string.ooops_network_error))
                    }


                }
            }

        }
    }


    fun setRongConfig() {
        RongConfigUtil.updatePortrait()
        RongConfigUtil.configurationRong()
    }


    override val networkErrorContainer: LinearLayout
        get() = network_error_container
    override val networkErrorTryAgain: TextView
        get() = network_error_try_again
}